/**
 * 根据attachBsId查询附件集合
 * Created by liaofei on 2014-5-20.
 */
define(["app",
    "resources/FileResource"], function (app) {

    var deps = ["$compile", "FileResource"];

    function directive ($compile, FileResource) {

        return {
            restrict: 'A',
            replace: true,
            link: function ($scope, element, attrs) {
                //attachBsId
                var fileAttachBsId = attrs.fileAttachs;
                var template = "";
                //console.log("fileAttachBsId="+fileAttachBsId);
                //当有附件集的时候，才需要去查询数据和组装下载地址
                if (fileAttachBsId) {
                    //附件接口
                    FileResource.getFiles(fileAttachBsId).success(function (data) {
                        var fileList = data.files;
                        for (var i = 0; i < fileList.length; i++) {
                            template += "<a href='restful/FileService/downFile?fileId=" + fileList[i].id + "'><span style='font-size: 12px;margin-left: 0'>" + fileList[i].fileName + "</span></a><br/>";
                        }
                        //console.log("template="+template);
                        element.html('').append($compile(template)($scope));
                    });

                }
            }
        };
    }

    directive.$inject = deps;
    app.lazy.directive("fileAttachs", directive);
});